<?php

namespace web\common\utils;

/**
* 计算奖金
*/
class RewardCalculation {


    private $level = [1=>0,2=>100,3=>200,4=>2000,5=>5000,6=>10000];


    /**
     * 获取父级
     */
    private function getPath($user_id,$level=false){
        $memberAccount = new \addons\member\model\MemberAccountModel();
        if($level){
            $path = $memberAccount->where('id',$user_id)->value('pid_path');
            $ids  = $memberAccount->field('m.id,m.level,a.level p_level,m.pid')->alias('m')->join('member_account a','a.id=m.pid','left')->where(['m.id'=>['in',$path],'m.level'=>['>',0]])->order('m.id desc')->select();
            return $ids;
        }else{
            $path = $memberAccount->where('id',$user_id)->value('pid_path');
            return explode(',', $path);
        }
    }

    /**
     * 获取等级
     */
    private function getLevel($user_id){
        return (new \addons\member\model\MemberAccountModel())->where('id',$user_id)->value('level');
    }

    /**
     * VIP计算 等级判断
     */
    public function VIPCalculation($user_id){
        $m = new \addons\member\model\MemberAccountModel();
        $info = $m->alias('m')->field('user_id,level,is_auth,contribution')->join('physical_contribution c','c.user_id=m.id','left')->where('m.id',$user_id)->find();
        $map['level'] = ['>',0];
        $list = (new \addons\member\model\LevelConfig())->where($map)->select();
        $level = 1;
        if($info['is_auth']==1){
            foreach ($list as $k => $v) {
                if($info['contribution']>=$v['need_contribution']){
                    $level = $v['level'];
                }
            }
            // echo 'user_id:'.$info['user_id'].'|level:'.$level."\n";
            if($level>$info['level']) $m->where('id',$info['user_id'])->update(['level'=>$level]);
        }
    }


    
}

?>
